package com.sansan.qiangji.controller.device;

import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;
import com.sansan.qiangji.form.device.stackerParam.StackerParamAddForm;
import com.sansan.qiangji.form.device.stackerParam.StackerParamEditForm;
import com.sansan.qiangji.form.device.stackerParam.StackerParamListForm;
import com.sansan.qiangji.realize.device.StackerParamReal;
import com.sansan.qiangji.resultVO.ResultVO;
import com.sansan.qiangji.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

@CrossOrigin()
@RestController
@RequestMapping("/stackerParam")
@Slf4j
public class StackerParamController {
    private StackerParamReal stackerParamReal;

    @Resource
    public void setStackerParamReal(StackerParamReal stackerParamReal){
        this.stackerParamReal = stackerParamReal;
    }

    @PostMapping("/stackerParamList")
    public ResultVO<Object> stackerParamList(@Valid @RequestBody StackerParamListForm stackerParamListForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("【堆垛机参数列表】参数不正确，stackerParamListForm={}", stackerParamListForm.toString());
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        return ResultVOUtil.success(stackerParamReal.stackerParamList(stackerParamListForm));
    }

    @PostMapping("/addStackerParam")
    public ResultVO<Object> addStackerParam(@Valid @RequestBody StackerParamAddForm stackerParamAddForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("【添加堆垛机参数】参数不正确，stackerParamAddForm={}", stackerParamAddForm.toString());
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        stackerParamReal.addStackerParam(stackerParamAddForm);
        return ResultVOUtil.success(null);
    }

    @PostMapping("/editStackerParam")
    public ResultVO<Object> editStackerParam(@Valid @RequestBody StackerParamEditForm stackerParamEditForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("【编辑堆垛机参数】参数不正确，stackerParamEditForm={}", stackerParamEditForm.toString());
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        stackerParamReal.editStackerParam(stackerParamEditForm);
        return ResultVOUtil.success(null);
    }

    @PostMapping("/deleteStackerParam")
    public ResultVO<Object> deleteStackerParam(@RequestParam(name = "stackerParamId") Integer stackerParamId) {
        stackerParamReal.deleteStackerParam(stackerParamId);
        return ResultVOUtil.success(null);
    }
}
